DrumAlert = LibStub("AceAddon-3.0"):NewAddon("DrumAlert", "AceConsole-3.0", "AceEvent-3.0","AceTimer-3.0")

local self = DrumAlert
_G["DrumAlert"] = DrumAlert
local df = nil
local tx = nil
local cooldown = nil
local drum = 29529
local skill = 35476
local g = 1
local db

local defaults = {
	profile = {
		x = nil,
		y = nil,
		lock = nil,
		style = {},
		scale = 1,
		alpha = 1,
	}
}

local optionFunc = {
	lock = function()
		if db.lock then
			df:SetBackdropColor(1,1,1,1)
			df:SetMovable(false)
			df:EnableMouse(false)
		else
			df:SetBackdropColor(0,1,0,1)
			df:SetMovable(true)
			df:EnableMouse(true)
		end
	end,
	scale = function() DrumAlert:SetScale() end,
	alpha = function() DrumAlert:SetAlpha() end,
}

local options = {
	name = "DrumAlert",
	type = "group",
	hidden = false,
	get = function(info)
		return DrumAlert.db.profile[info[#info]]
	end,
	set = function(info, value)
		DrumAlert.db.profile[info[#info]] = value
		local func = optionFunc[info[#info]]
		if func then
			func(info, value)
		end
	end,
	args = {
		lock = {
			name = "Lock",
			type = "toggle",
			desc = "Locks/unlocks the frame.Show when you enter combat.",
			order = 1,
		},
		scale = {
			name = "Scale",
			type = "range",
			desc = "Sets scale.",
			min = 0.2,
			max = 3,
			step = 0.05,
			isPercent = true,
			order = 2,
		},
		alpha = {
			name = "Alpha",
			type = "range",
			desc = "Sets alpha.",
			min = 0,
			max = 1,
			step = 0.05,
			isPercent = true,
			order = 3,
		},
	}
}

------------------------------
--      Initialization      --
------------------------------

function self:OnInitialize()
	self:Print("Initialize")
	self.db = LibStub("AceDB-3.0"):New("DrumAlertDB",defaults)
	db = self.db.profile
end

------------------------------
--       Frame Setup        --
------------------------------

function self:OnEnable()
  	if df then return end
	
	LibStub("AceConfig-3.0"):RegisterOptionsTable("DrumAlert", options)
	LibStub("AceConfigDialog-3.0"):AddToBlizOptions("DrumAlert", "DrumAlert")
	
		df = CreateFrame("Frame", "DrumAlertFrame", UIParent)
		df:SetBackdrop({bgFile = GetItemIcon(29529),insets = { left = 0, right = 0, top = 0, bottom = 0 }})
		df:SetFrameStrata("BACKGROUND")
		df:SetPoint("CENTER", UIParent, "CENTER", 0, 0)
		df:SetBackdropColor(1,1,1,1)
		df:SetWidth(64)
		df:SetHeight(64)
		df:EnableMouse(true)
		df:RegisterForDrag("LeftButton")
		df:SetMovable(true)
		df:SetScript("OnDragStart", function() this:StartMoving() end)
		df:SetScript("OnDragStop", function()
			this:StopMovingOrSizing()
			self:SavePosition()
		end)
		df:Hide()
		
		tx = df:CreateTexture("DrumAlerttx")
		tx:SetPoint("CENTER", df, "CENTER", 0, 0)
		tx:SetWidth(74)
		tx:SetHeight(74)
		tx:SetTexture("Interface\\AddOns\\SurgeTrack\\border.tga")
		tx:Show()
		
		cooldown = CreateFrame("Cooldown", nil, df, "CooldownFrameTemplate")
		self.cooldown = cooldown
		cooldown:SetPoint("CENTER", df, "CENTER", 0, 0)
		cooldown:SetWidth(36)
		cooldown:SetHeight(36)
		cooldown:Hide()
		if db.lock then
			df:SetMovable(false)
			df:EnableMouse(false)
		end
		self:SetScale()
		self:RegisterEvent("BAG_UPDATE_COOLDOWN")
		self:RegisterEvent("PLAYER_REGEN_DISABLED")
		self:RegisterEvent("PLAYER_REGEN_ENABLED")
end

------------------------------
--      Event/Function      --
------------------------------

function self:SavePosition()
	if not df then return end
	local s = df:GetEffectiveScale()
	db.x = df:GetLeft() * s
	db.y = df:GetTop() * s
end

function self:SetScale()
	df:SetScale(DrumAlert.db.profile.scale)
	local x = db.x
	local y = db.y
	if x and y then
		local s = df:GetEffectiveScale()
		df:ClearAllPoints()
		df:SetPoint("TOPLEFT", UIParent, "BOTTOMLEFT", x / s, y / s)
	end
end

function self:SetAlpha()
	df:SetAlpha(DrumAlert.db.profile.alpha)
end

function self:PLAYER_REGEN_DISABLED()
	df:Show()
end

function self:PLAYER_REGEN_ENABLED()
	df:Hide()
end

function self:BAG_UPDATE_COOLDOWN()
	if ( g ) then
		local start, duration, enable = GetItemCooldown(drum)
		if ( start > 0 and duration > 2 and enable == 1) then
		CooldownFrame_SetTimer(cooldown, start, duration, enable)
		df:SetBackdropColor(1,1,1,1)
		df:Show()
		self:Print("使用"..select(2,GetItemInfo(drum)))
		SendChatMessage("使用"..GetSpellLink(skill)..",持续30s","PARTY")
		self:ScheduleTimer("TimerCD", duration)
		self:ScheduleTimer("TimerLAST", 30)
		g = nil
		end
	end
end

function self:TimerCD()
self:Print(select(2,GetItemInfo(drum)).."冷却")
SendChatMessage("的"..select(2,GetItemInfo(drum)).."非常不低调地冷却了 讨厌~~","EMOTE")
PlaySoundFile("Sound\\interface\\AuctionWindowClose.wav")
df:SetBackdropColor(1,0.5,0.5,1)
g = 1
end

function self:TimerLAST()
self:Print(select(2,GetItemInfo(drum)).."-")
SendChatMessage(GetSpellLink(skill).."结束,下个接上","PARTY")
end
